package com.ruoyi.project.system.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.system.domain.Order;
import com.ruoyi.project.system.service.IOrderService;
import org.apache.catalina.connector.Response;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.OrderDetails;
import com.ruoyi.project.system.service.IOrderDetailsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 订单详情Controller
 *
 * @author tsxuwj
 * @date 2020-06-22
 */
@RestController
@RequestMapping("/system/details")
public class OrderDetailsController extends BaseController {
    @Autowired
    private IOrderDetailsService orderDetailsService;

    /**
     * 查询订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderDetails orderDetails) {
        startPage();
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsList(orderDetails);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:export')")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrderDetails orderDetails) {
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsListTSXUWJ(orderDetails);
        ExcelUtil<OrderDetails> util = new ExcelUtil<OrderDetails>(OrderDetails.class);
        return util.exportExcel(list, "details");
    }


    /**
     * 获取订单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(orderDetailsService.selectOrderDetailsById(id));
    }

    /**
     * 新增订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderDetails orderDetails) {
        return toAjax(orderDetailsService.insertOrderDetails(orderDetails));
    }

    /**
     * 修改订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderDetails orderDetails) {
        return toAjax(orderDetailsService.updateOrderDetails(orderDetails));
    }

    /**
     * 删除订单详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderDetailsService.deleteOrderDetailsByIds(ids));
    }

    /**
     * 新增订单详情-自写
     */
    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping("/TSXUWJ")
    public AjaxResult addTSXUWJ(@RequestBody OrderDetails orderDetails) {
        return toAjax(orderDetailsService.insertOrderDetailsTSXUWJ(orderDetails));
    }

    /**
     * 修改订单详情-自写
     */
    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PutMapping("/TSXUWJ")
    public AjaxResult editTSXUWJ(@RequestBody OrderDetails orderDetails) {
        return toAjax(orderDetailsService.updateOrderDetailsTSXUWJ(orderDetails));
    }

    /**
     * 删除订单详情-自写
     */
    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/TSXUWJ/{id}")
    public AjaxResult removeTSXUWJ(@PathVariable Long id) {
        return toAjax(orderDetailsService.deleteOrderDetailsByIdTSXUWJ(id));
    }

    /**
     * 查询订单详情列表-自写
     */
    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list/TSXUWJ")
    public TableDataInfo listTSXUWJ(OrderDetails orderDetails) {
        startPage();
        List<OrderDetails> list = orderDetailsService.selectOrderDetailsListTSXUWJ(orderDetails);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表-自写
     */
    @PreAuthorize("@ss.hasPermi('system:details:export')")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export/TSXUWJ")
    public AjaxResult exportTSXUWJ(OrderDetails orderDetails) throws IOException {
        return orderDetailsService.exportOrderDetailsTSXUWJ(orderDetails);
    }

}
